package com.gosophia.conferenceManagement.scheme.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.gosophia.commons.OrderCondition;
import com.gosophia.commons.Page;
import com.gosophia.commons.PropertyFilter;
import com.gosophia.commons.exception.BusinessException;
import com.gosophia.conferenceManagement.common.service.impl.ConferenceHandleServiceImpl;
import com.gosophia.conferenceManagement.scheme.dao.SchemeTransientStoreDao;
import com.gosophia.conferenceManagement.scheme.entity.SchemeTransientStore;
import com.gosophia.conferenceManagement.scheme.service.ConferenceSchemeService;
import com.gosophia.taskManagement.dao.MissionDao;

/**
 * 会议方案接口实现
 * @author Administrator
 *@since 2011-6-2
 */
@Service("conferenceSchemeService")
@Transactional
public class ConferenceSchemeServiceImpl extends ConferenceHandleServiceImpl implements ConferenceSchemeService{

	@Autowired
	private SchemeTransientStoreDao schemeTransientStoreDao;
	
	@Override
	public Long saveOrUpdateSchemeTransientStore(
			SchemeTransientStore schemeTransientStore) {
		Assert.isTrue(schemeTransientStore != null, "暂存实体不能为null");
		Long id ;
		if(schemeTransientStore.getSchemeTransientStoreId()==null){
		    id = (Long)schemeTransientStoreDao.add(schemeTransientStore);
		}else{
		    id = schemeTransientStore.getSchemeTransientStoreId();
			schemeTransientStoreDao.update(schemeTransientStore);
		}
		return id;
		
	}


	@Override
	public void deleteSchemeTransientStore(
			Long schemeTransientStoreId) throws BusinessException {
		Assert.isTrue(schemeTransientStoreId != null, "暂存实体Id不能为null");
		schemeTransientStoreDao.delete(schemeTransientStoreId);
	}


	@Override
	public Page<SchemeTransientStore> findSchemeTransientStoreByPage(Long userInfoId,Page<SchemeTransientStore> page, List<PropertyFilter> propertyFilters,
            List<OrderCondition> orderConditions) throws BusinessException{
		Assert.notNull(userInfoId,"userInfoId不能为null");
        Assert.notNull(page,"page不能为null");
        Assert.isTrue(page.getPageSize()>0,"pageSize必须大于0");
        Assert.isTrue(page.getPageNo()>0,"pageNo必须大于0");
        
        orderConditions = this.getSchemeTransientStroeOrderConditions(orderConditions);
        if(propertyFilters==null){
            propertyFilters = new ArrayList<PropertyFilter>();
        }
        //添加条件
        propertyFilters.add(new PropertyFilter("creator.userInfoId",userInfoId));
        page = schemeTransientStoreDao.getPageByProperties(page, propertyFilters, orderConditions);
        //备注--还没有写完要根据页面来查有什么条件
        return page;
	}
	
	@Override
	public Page<SchemeTransientStore> findSchemeTransientStoreSimplePage(
			Long userInfoId, Page<SchemeTransientStore> page,String searchStr,
			List<PropertyFilter> propertyFilters,
			List<OrderCondition> orderConditions) throws BusinessException {
		Assert.notNull(userInfoId,"userInfoId不能为null");
        Assert.notNull(page,"page不能为null");
        Assert.isTrue(page.getPageSize()>0,"pageSize必须大于0");
        Assert.isTrue(page.getPageNo()>0,"pageNo必须大于0");
        
        orderConditions = this.getSchemeTransientStroeOrderConditions(orderConditions);
        if(propertyFilters==null){
            propertyFilters = new ArrayList<PropertyFilter>();
        }
        //添加待办条件
        propertyFilters.add(new PropertyFilter("creator.userInfoId",userInfoId));
        schemeTransientStoreDao.getPageBySimpleSearching(page, searchStr, null, propertyFilters, orderConditions);
		return page;
	}
	
	private List<OrderCondition> getSchemeTransientStroeOrderConditions(
            List<OrderCondition> orderConditions) {
        if(orderConditions != null && orderConditions.size()>=1) {
            for(int i = orderConditions.size() - 1;i >= 0;--i)
            {
                OrderCondition orderCondition = orderConditions.get(i);
                if(orderCondition.getPropertyName() == null || orderCondition.getPropertyName().length() <= 0
                        || orderCondition.getOrder() == null || orderCondition.getOrder().length() <= 0) 
                    orderConditions.remove(i);
            }
        }
        
        if(orderConditions == null || orderConditions.size() <= 0)
        {
            orderConditions = new ArrayList<OrderCondition>();
            //默认排序：重要度降序；计划结束时间降序
            orderConditions.add(new OrderCondition("importance.orderIndex", false));
            orderConditions.add(new OrderCondition("planEndDate",true)); 
        }
        return orderConditions;
    }


	@Override
	public SchemeTransientStore findTransientStoreByJobId(Long jobId)
			throws BusinessException {
		Assert.notNull(jobId,"jobId不能为null");
		SchemeTransientStore schemeTransientStore = schemeTransientStoreDao.getUniqueByNamedQuery("conferenceSchemeService.findTransientStoreByJobId", jobId);
		return schemeTransientStore;
	}


	@Override
	public SchemeTransientStore findTransientStoreById(Long id)
			throws BusinessException {
		Assert.notNull(id, "id不能为null");
		SchemeTransientStore schemeTransientStore = schemeTransientStoreDao.get(id);
		return schemeTransientStore;
	}

}
